package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.elasticsearch.domain.DestinationEs;
import cn.wolfcode.wolf2w.elasticsearch.domain.StrategyEs;
import cn.wolfcode.wolf2w.elasticsearch.domain.TravelEs;
import cn.wolfcode.wolf2w.elasticsearch.domain.UserInfoEs;
import cn.wolfcode.wolf2w.elasticsearch.query.SearchQueryObject;
import cn.wolfcode.wolf2w.elasticsearch.service.*;
import cn.wolfcode.wolf2w.elasticsearch.vo.SearchResultVO;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.ParamMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;



@RestController
public class SearchController {

    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private ISearchService searchService;


    @GetMapping("/q")
    public Object search(SearchQueryObject qo) throws UnsupportedEncodingException {

        //url解码
        String decode = URLDecoder.decode(qo.getKeyword(), "UTF-8");
        qo.setKeyword(decode);

        switch (qo.getType()) {
            case SearchQueryObject.TYPE_DEST:
                return searchDest(qo);
            case SearchQueryObject.TYPE_STRATEGY:
                return searchStrategy(qo);
            case SearchQueryObject.TYPE_TRAVEL:
                return searchTravel(qo);
            case SearchQueryObject.TYPE_USER:
                return searchUser(qo);
            default:
                return searchAll(qo);
        }

    }


    //查询目的地
    private Object searchDest(SearchQueryObject qo) {
        //查询keyword对应的目的地是否存在
        Destination destination = destinationService.queryByName(qo.getKeyword());

        //如果存在,查询该目的地下的攻略,游记,用户
        SearchResultVO vo = new SearchResultVO();
        if (destination!=null){
            List<Strategy> sts = strategyService.queryByDestId(destination.getId());
            List<Travel> ts = travelService.queryByDestId(destination.getId());
            List<UserInfo> us = userInfoService.queryByCity(destination.getName());
            vo.setStrategys(sts);
            vo.setTravels(ts);
            vo.setUsers(us);

            vo.setTotal(sts.size()+ts.size()+us.size()+0L);
        }
        return JsonResult.success(new ParamMap().put("qo", qo).put("dest",destination).put("result", vo));

    }


    //查询用户
    private Object searchUser(SearchQueryObject qo) {
        return JsonResult.success(new ParamMap().put("qo", qo).put("page",this.queryUserInfoPage(qo)));
    }

    //查询游记
    private Object searchTravel(SearchQueryObject qo) {
        return JsonResult.success(new ParamMap().put("qo", qo).put("page",this.queryTravelPage(qo)));
    }
    //查询攻略
    private Object searchStrategy(SearchQueryObject qo) {
        //攻略的全文搜索和高亮
        return JsonResult.success(new ParamMap().put("qo", qo).put("page",this.queryStrategyPage(qo)));
    }

    //查询全部
    private Object searchAll(SearchQueryObject qo){
        Page<UserInfo> us = this.queryUserInfoPage(qo);
        Page<Strategy> sts = this.queryStrategyPage(qo);
        Page<Travel> ts = this.queryTravelPage(qo);
        Page<Destination> ds = this.queryDestinationPage(qo);
        SearchResultVO vo = new SearchResultVO();
        vo.setStrategys(sts.getContent());
        vo.setTravels(ts.getContent());
        vo.setUsers(us.getContent());
        vo.setDests(ds.getContent());
        vo.setTotal(sts.getTotalElements()+ts.getTotalElements()+us.getTotalElements()+ds.getTotalElements());

        //qo
        //result
        return JsonResult.success(new ParamMap().put("qo", qo).put("result", vo));
    }

     //代码抽取
     private Page<Strategy> queryStrategyPage(SearchQueryObject qo){
        return searchService.searchWithHighlight(StrategyEs.INDEX_NAME,
                StrategyEs.TYPE_NAME, Strategy.class, qo, "title", "subTitle", "summary");
     }
     private Page<Travel> queryTravelPage(SearchQueryObject qo){
        return searchService.searchWithHighlight(TravelEs.INDEX_NAME,
                TravelEs.TYPE_NAME, Travel.class, qo, "title", "summary");
     }
     private Page<UserInfo> queryUserInfoPage(SearchQueryObject qo){
        return searchService.searchWithHighlight(UserInfoEs.INDEX_NAME,
                UserInfoEs.TYPE_NAME, UserInfo.class, qo, "city", "info", "nickname");
     }
     private Page<Destination> queryDestinationPage(SearchQueryObject qo){
        return searchService.searchWithHighlight(DestinationEs.INDEX_NAME,
                DestinationEs.TYPE_NAME, Destination.class, qo, "name", "info");
     }


}
